<?php

/**
 * Created by PhpStorm.
 * User: dungnt13
 * Date: 12/4/14
 * Time: 2:24 PM
 */
class Dictionary extends Eloquent {

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    public function category() {
        return $this->belongsTo('Category', 'category_id');
    }

    //danh sách các từ được sắp xếp theo thứ tự alphab
    public function listDataArrangement($data_input) {
        $result = array();
        $index = 0;
        $start_index = 0;
        $last_index = 0;
        $array_number = array();
        foreach ($data_input as $value) {
            if ($data_input[$index]['title'] == '') {
                $start_index = $index + 1;
            } else {
                if (is_numeric(substr(trim($data_input[$index]['title']), 0, 1))) {
                    array_push($array_number, $value);
                    $start_index = $index + 1;
                } else {
                    if (strtoupper(substr(trim($data_input[$start_index]['title']), 0, 1)) != strtoupper(substr(trim($value['title']), 0, 1))) {
                        $last_index = $index - 1;
                        array_push($result, array(substr(trim($data_input[$start_index]['title']), 0, 1), array_slice($data_input, $start_index, 1 + $last_index - $start_index)));
                        $start_index = $index;
                    }
                    if ($index == count($data_input) - 1) {
                        array_push($result, array(substr(trim($data_input[$start_index]['title']), 0, 1), array_slice($data_input, $start_index, 1 + $index - $start_index)));
                        break;
                    }
                }
            }

            $index++;
        }
        if (count($array_number) > 0)
            array_push($result, array("0...9", $array_number));
        Return $result;
    }

    //danh sách danh mục từ điển
    public function Listcategories($id_category, $list_categories) {
        $category = Category::where('id', '=', $id_category)->select('id', 'title', 'parent_id')->get();
        if ($category->count() > 0) {
            $category = $category->first();
            array_push($list_categories, $category);
            $id_parent = $category->parent_id;
            if (Category::where('id', '=', $id_parent)->select('id', 'title', 'parent_id')->get()->count()>0 && $id_parent > 0) {
                $list_categories = $this->Listcategories($id_parent, $list_categories);
            }
        }
        return $list_categories;
    }

    //danh sách danh mục gốc 
    public function ListCategoryRoot() {
        $key = 'list_categories_root' . date('d-m-Y');
        $list = array();
        if (Cache::has($key)) {
            $list = Cache::get($key);
        } else {
            $list = Category::Where('type', '=', 'dictionary')
                    ->where('parent_id', '=', '0')
                    ->get();
            Cache::put($key, $list, 60);
        }
        return $list;
    }

    //danh sách menuleft
    public function Listword_Left() {
        $key = 'list_word_left' . date('d-m-Y');
        $list = array();
        if (Cache::has($key)) {
            $list = Cache::get($key);
        } else {
            $words = Dictionary::where('status', '=', 'published')
                            ->select('id', 'title')
                            ->orderBy('title', 'ASC')
                            ->get()->toArray();
            $list = $this->listDataArrangement($words);
            Cache::put($key, $list, 60);
        }
        return $list;
    }

}
